//
//  GLMiniMusicView.m
//  GLMusicBox
//
//  Created by 高磊 on 2017/11/10.
//  Copyright © 2017年 高磊. All rights reserved.
//

#import "GLMiniMusicView.h"
#import "AppDelegate.h"
#import "ZYAudioManager.h"
#import "ZYMusicTool.h"

#import "ZYPlayingViewController.h"

static CGFloat const kShowBarHeight = 54;
static CGFloat const kMiniMusicImageWidth = 36;
static CGFloat const kMiniMusicImageHeight = 36;
static CGFloat const kMiniMusicPlayWidth = 30;
static CGFloat const kMiniMusicPlayHeight = 30;

@interface GLMiniMusicView()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;

@end

@implementation GLMiniMusicView

+ (instancetype)shareInstance
{
    static GLMiniMusicView *miniMusicView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        miniMusicView = [[GLMiniMusicView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, [LXAutoLength lx_L:kShowBarHeight])];
    });
    
    return miniMusicView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kAppThemeColor;
        [self drawAroundViewWithLocation:DrawTop color:kAppGrayTextColor insets:UIEdgeInsetsMake(0.5, 0, 0, 0)];
        [self addViews];
        [self addViewConstraint];
        
        self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTouch:)];
        self.tapGesture.delegate = (id)self;
        [self addGestureRecognizer:self.tapGesture];
    }
    return self;
}

- (void)addViews
{
    [self addSubview:self.imageView];
    [self addSubview:self.titleLable];
    [self addSubview:self.palyButton];
    [self addSubview:self.nextButton];
}

- (void)addViewConstraint
{
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake([LXAutoLength lx_L:kMiniMusicImageWidth], [LXAutoLength lx_L:kMiniMusicImageHeight]));
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left).offset(20);
    }];

    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageView.mas_right).offset(20);
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.palyButton.mas_left).offset(-20);
    }];

    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake([LXAutoLength lx_L:kMiniMusicPlayWidth], [LXAutoLength lx_L:kMiniMusicPlayHeight]));
        make.right.equalTo(self.mas_right).offset(-30);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [self.palyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake([LXAutoLength lx_L:kMiniMusicPlayWidth], [LXAutoLength lx_L:kMiniMusicPlayHeight]));
        make.right.equalTo(self.nextButton.mas_left).offset(-20);
        make.centerY.equalTo(self.mas_centerY);
    }];
}


#pragma mark == UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    return YES;
}


#pragma mark == event response
- (void)tapTouch:(UITapGestureRecognizer *)tap
{
    UIViewController *vc = nil;
    if ([APP.window.rootViewController isKindOfClass:[UINavigationController class]]) {
        vc = ((UINavigationController *)APP.window.rootViewController).topViewController;
    } else if ([APP.window.rootViewController isKindOfClass:[UIViewController class]]){
        vc = ((UIViewController *)APP.window.rootViewController);
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(miniMusicView:tapDown:)]) {
        [self.delegate miniMusicView:self tapDown:tap];
    }
}

- (void)play:(UIButton *)sender
{
    if ([[ZYAudioManager defaultManager] mpcVC].nowPlayingItem == nil) {
        return;
    }
    if (sender.isSelected == NO) {
        sender.selected = YES;
    } else{
        sender.selected = NO;
    }
    [[ZYAudioManager defaultManager] playPauseMusic];
}

- (void)next:(UIButton *)sender
{
    [[ZYAudioManager defaultManager] nextMusic];
    if (self.delegate && [self.delegate respondsToSelector:@selector(miniMusicViewNext:)]) {
        [self.delegate miniMusicViewNext:self];
    }
}
#pragma mark == public method
- (void)showView
{
    [APP.window addSubview:self];
    self.alpha = 0;
    NSInteger offsetHeight = kScreenHeight > 800 ? 39 : 0;
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, kShowBarHeight));
        make.left.equalTo(APP.window.mas_left);
        make.bottom.equalTo(APP.window.mas_bottom).offset(-kShowBarHeight - offsetHeight);
    }];

    [UIView animateWithDuration:1 animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hiddenView
{
    [self removeFromSuperview];
}

- (void)showMinViewDetail
{
    MPMediaItem *item = [ZYAudioManager defaultManager].mpcVC.nowPlayingItem;
    if (item) {
        self.titleLable.text = [item valueForProperty:MPMediaItemPropertyTitle];
    } else {
        self.titleLable.text = @"Not Playing";
    }
    MPMusicPlayerController *mpc = [MPMusicPlayerController applicationMusicPlayer];
    MPMusicPlaybackState state = [mpc playbackState];
    if (state == MPMusicPlaybackStatePlaying) {
        self.palyButton.selected = YES;
    } else {
        self.palyButton.selected = NO;
    }
}
#pragma mark == 懒加载
- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [UIImage imageNamed:@"background"];
        //设置圆角 当然可以选择设置cornerRadius
        _imageView.layer.cornerRadius = 2;
        _imageView.layer.masksToBounds = YES;
    }
    return _imageView;
}

- (UILabel *)titleLable
{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc] init];
        _titleLable.font = [UIFont systemFontOfSize:15];
        _titleLable.textColor = kAppNormalTextColor;
        _titleLable.text = @"Not Playing";
    }
    return _titleLable;
}

- (UIButton *)palyButton
{
    if (!_palyButton) {
        _palyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_palyButton setImage:[UIImage imageNamed:@"start"] forState:UIControlStateNormal];
        [_palyButton setImage:[UIImage imageNamed:@"停止"] forState:UIControlStateSelected];
        [_palyButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _palyButton;
}

- (UIButton *)nextButton
{
    if (!_nextButton) {
        _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextButton setImage:[UIImage imageNamed:@"next"] forState:UIControlStateNormal];
        [_nextButton setImage:[UIImage imageNamed:@"next"] forState:UIControlStateSelected];
        [_nextButton addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextButton;
}

@end
